<?php

class Account extends API_Controller {

    private $graph = 'https://graph.facebook.com';

    public function __construct() {
        parent::__construct();
        $this->load->model('account_model');
        $this->load->helper('url');
    }

    public function map() {
        $params = $this->params;
        if (empty($params['fb_id']) == TRUE AND empty($params['fb_id_map']) == TRUE) {
            $this->response('INVALID_PARAMS');
        }
        
        $facebook_info = $this->account_model->get($params['fb_id_map']);
        if(empty($facebook_info) == FALSE){
            $this->response('ACCOUNT_FACEBOOK_MAP_ALREADY');
        }
        
        $link = $this->graph . '/' . $params['fb_id_map'];
        $account_info = json_decode(file_get_contents($link), TRUE);
        $data_insert = array(
            'fb_id' => $params['fb_id'],
            'fb_id_map' => $params['fb_id_map'],
            'fullname' => $account_info['name'],
        );                
        $this->account_model->create($data_insert);
        redirect(base_url('account'));
    }

}
